﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Utility;

namespace Presentation
{
    public partial class MainSearch : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)//IsPostBack＝false the first request
            {

                this.RoleSearchGridView.Attributes.Add("SortExpression", "LeName");
                this.RoleSearchGridView.Attributes.Add("SortDirection", "ASC");
                Session["sortexpressin"] = "LeName";
                Session["sortdirection"] = "ASC";
                Session["pageindex"] = 1;
                Session["pagecount"] = 1;

                //AuthorityCheckBoxList.DataSource = BLL.IAuthorityShow.Search();
                //IAuthorityShow authority = Factory.SearchTnfo<IAuthorityShow>();
                //AuthorityCheckBoxList.DataSource = authority.Search();
                //AuthorityCheckBoxList.DataBind();

                IStatusRepository status = RepositoryFactory.SearchTnfo<IStatusRepository>();
                
                StatusDropDownList.DataSource = status.Search();
                StatusDropDownList.DataTextField = "StatusName";
                StatusDropDownList.DataValueField = "StatusId";
                StatusDropDownList.DataBind();
                StatusDropDownList.Items.Add("Please Select");
                StatusDropDownList.SelectedValue = "Please Select";

                IAuthorityRepository authority = RepositoryFactory.SearchTnfo<IAuthorityRepository>();
                AuthorityCheckBoxList.DataSource = authority.Search();
                AuthorityCheckBoxList.DataTextField = "AuthorityName";
                AuthorityCheckBoxList.DataValueField = "AuthorityId";                   
                AuthorityCheckBoxList.DataBind();
            }

            // the function will execute agin when refresh


            //IStatusShow status = Factory.SearchTnfo<IStatusShow>();
            //StatusDropDownList.DataSource = status.Search();                     
            //StatusDropDownList.DataBind();
            //StatusDropDownList.DataSource = BLL.StatusShow.Search();



        }

        protected void GenerateReportButton_Click(object sender, EventArgs e)
        {
            int pageSize = RoleSearchGridView.PageSize;
            Session["pagesize"] = pageSize;
            //int pageIndex = RoleSearchGridView.PageIndex;
            //RoleSearchGridView.SortExpression.ToString(), RoleSearchGridView.SortDirection.ToString(),
            string sortExpression = Session["sortexpressin"].ToString();
            string sortDirection = Session["sortdirection"].ToString();

            gridBind(sortExpression, sortDirection, pageSize, 1);

        }

        protected void StatusDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


        protected void RoleSearchGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //分页完成之前 
            RoleSearchGridView.PageIndex = e.NewPageIndex;
            int pageSize = RoleSearchGridView.PageSize;
            int pageIndex = RoleSearchGridView.PageIndex;
            gridBind("lename", "ASC", pageSize, pageIndex);

        }


        protected void RoleSearchGridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            // 从事件参数获取排序数据列
            string sortExpression = e.SortExpression.ToString();
            // 假定为排序方向为“顺序”
            string sortDirection = this.RoleSearchGridView.Attributes["SortDirection"].ToString();
            // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
            if (sortExpression == this.RoleSearchGridView.Attributes["SortExpression"])
            {
                //获得下一次的排序状态

                string a = this.RoleSearchGridView.Attributes["SortDirection"].ToString();
                //sortDirection = (a== sortDirection ? "DESC" : "ASC");
                if (a == "ASC")
                {
                    sortDirection = "DESC";
                }
                else
                {
                    sortDirection = "ASC";
                }
            }

            // 重新设定GridView排序数据列及排序方向
            this.RoleSearchGridView.Attributes.Remove("SortExpression");
            this.RoleSearchGridView.Attributes.Remove("SortDirection");
            this.RoleSearchGridView.Attributes.Add("SortExpression", sortExpression);
            this.RoleSearchGridView.Attributes.Add("SortDirection", sortDirection);
            int pageSize = RoleSearchGridView.PageSize;
            Session["sortexpressin"] = sortExpression;
            Session["sortdirection"] = sortDirection;
            //int pageIndex = RoleSearchGridView.PageIndex + 1;
            // RoleSearchGridView.Sort(sortExpression, RoleSearchGridView.SortDirection);
            gridBind(sortExpression, sortDirection, pageSize, 1);



        }

        public void gridBind(string sortExpression, string sortDirection, int pageSize, int pageIndex)
        {
            string roleName = RoleNameTextBox.Text.Trim();
            string userName = UserTextBox.Text.Trim();
            string authorities = "";
            string firstName = "";
            string lastName = "";
            string status = StatusDropDownList.SelectedItem.Text;
            string[] nameSplit = userName.Split(' ');
            // List<Role> RolePageNum = new List<Role>();
            int pageCnt = 0;
            Session["pageindex"] = pageIndex;

            if (nameSplit.Length == 1)
            {
                firstName = nameSplit[0];
                lastName = "";
            }
            else if (nameSplit.Length == 2)
            {
                firstName = nameSplit[0];
                lastName = nameSplit[1];
            }
            else
            {

            }

            List<string> authorityList = new List<string>();

            for (int i = 0; i < AuthorityCheckBoxList.Items.Count; i++)
            {
                if (AuthorityCheckBoxList.Items[i].Selected)
                    authorityList.Add(AuthorityCheckBoxList.Items[i].Text);
            }

            if (authorityList.Count > 0)
            {
                for (int j = 0; j < authorityList.Count - 1; j++)
                {
                    authorities += authorityList[j];
                    authorities += ",";
                }
                int lastId = authorityList.Count - 1;
                authorities += AuthorityCheckBoxList.Items[lastId].Text;
            }

            IRoleRepository role = RepositoryFactory.SearchTnfo<IRoleRepository>();
            RoleSearchGridView.DataSource = role.Search(roleName, firstName, lastName, status, authorities, sortExpression, sortDirection, pageSize, pageIndex, out pageCnt);

            //RoleSearchGridView.DataSource = BLL.RoleSearch.Search(roleName, firstName, lastName, status, authorities, sortExpression, sortDirection, pageSize, pageIndex, out pageCnt);

            RoleSearchGridView.DataBind();

            //RolePageNum = BLL.RoleSearch.Search(roleName, firstName, lastName, status, authorities, sortExpression, sortDirection, pageSize, pageIndex);



            if (pageCnt != 0)
            {
                //if(RolePageNum[0].PageCount > 0)出错

                Session["pagecount"] = pageCnt;

                if (pageCnt == 1)
                {
                    IsOrNotShow(false);

                }

                else
                {
                    IsOrNotShow(true);

                    lblCount.Text = Session["pagecount"].ToString();
                    lblCurrentInfo.Text = pageIndex.ToString();
                }
            }
            else
            {
                IsOrNotShow(false);
            }


            //RoleSearchGridView.PageCount是只读属性;
        }

        protected void PageIndex_Changed(object sender, EventArgs e)
        {

            string sortExpression = Session["sortexpressin"].ToString();
            string sortDirection = Session["sortdirection"].ToString();
            int pageSize = Convert.ToInt32(Session["pagesize"]);
            int pageIndex = Convert.ToInt32(Session["pageindex"]);
            int pageCount = Convert.ToInt32(Session["pagecount"]);

            LinkButton lb = (LinkButton)sender;
            if (lb.ID == "lbtnFirst")
            {
                gridBind(sortExpression, sortDirection, pageSize, 1);
            }
            else if (lb.ID == "lbtnPrev")
            {
                if (pageIndex > 1)
                {
                    gridBind(sortExpression, sortDirection, pageSize, pageIndex - 1);
                }


            }
            else if (lb.ID == "lbtnNext")
            {
                if (pageIndex < pageCount)
                {
                    gridBind(sortExpression, sortDirection, pageSize, pageIndex + 1);
                }
            }
            else
            {

                gridBind(sortExpression, sortDirection, pageSize, pageCount);
            }

        }

        //判断GridView控件是否可见
        public void IsOrNotShow(bool falseOrTrue)
        {
            lbtnFirst.Visible = falseOrTrue;
            lbtnLast.Visible = falseOrTrue;
            lbtnPrev.Visible = falseOrTrue;
            lbtnNext.Visible = falseOrTrue;
            lbtnLast.Visible = falseOrTrue;
            lblCount.Visible = falseOrTrue;
            lblCurrentInfo.Visible = falseOrTrue;
            Label1.Visible = falseOrTrue;
        }
    }
}